<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

</html>
<script>
    const getNearestDateIndex = (targetDate, dates) => {
        if (!targetDate || !dates) {
            throw new Error('Argument(s) is illegal');
        }
        if (!dates.length) {
            return -1
        }
        const distances = dates.map(date => Math.abs(date - targetDate))
        return distances.indexOf(Math.min(...distances))
    }
    const targetDate = new Date(2019, 7, 20)
    const dates = [
        new Date(2018, 0, 1),
        new Date(2019, 0, 1),
        new Date(2020, 0, 1),
    ]
    console.log(dates);
    console.log(getNearestDateIndex(targetDate, dates))

    //map不改变原来数组
    //reduce改变
</script>